package com.itheima.day15.TCP.test1;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {
    public static void main(String[] args) {
        ServerSocket ss = null;
        InputStream is = null;
        try {
            // 1.创建Socket对象(ServletSocket)
            ss = new ServerSocket(10001);

            System.out.println("等待连接");
            // 2.等待客户端连接
            Socket accept = ss.accept();
            System.out.println("连接完成");

            // 3.获取输入流对象
            is = accept.getInputStream();

            int len;
            while ((len = is.read()) != -1) {
                System.out.print((char) len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 4.释放资源
            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
